import AsyncSession from '../services/AsyncSession';
import * as types from '../constants/actionTypes';
import * as config from '../constants/apiConfig';
import api from '../services/api';
import { Navigation } from '../views/global/navigation/index';

function session_change(session,callback){
    return async (dispatch) =>{
        console.log('session_change======session',session);
        var {ticket, userId, reload,group}=session;
        let res = await api.post('/housekeeper/findUserById',{uid:userId,ticket:ticket});
        console.log('session_change======res',res);
        if (res.cd != "0") {
            dispatch({type:types.CHANGE,
                          session:null});
        }else{
            if (res.data && res.data.productKey) {
                console.log('session_change======res.productKey=',res.data.productKey);
                let webBaseUrl = config.WEB_BASE_URL;
                switch(webBaseUrl) {
                    case config.HC_WEB_BASE_URL:
                        if (res.data.productKey != "HeChang") {
                            dispatch({type:types.CHANGE,session:null});
                            callback("登录账号所属公众号与本APP不符！");
                            return;
                        }
                        break;
                    case config.HLZ_WEB_BASE_URL:
                        if (res.data.productKey != "HuLiZhan") {
                            dispatch({type:types.CHANGE,session:null});
                            callback("登录账号所属公众号与本APP不符！");
                            return;
                        }
                        break;
                    case config.LXS_WEB_BASE_URL:
                        if (res.data.productKey != "LiXiaoShuang") {
                            dispatch({type:types.CHANGE,session:null});
                            callback("登录账号所属公众号与本APP不符！");
                            return;
                        }
                        break;
                    case config.CYJ_WEB_BASE_URL:
                        if (res.data.productKey != "ChangYangJi") {
                            dispatch({type:types.CHANGE,session:null});
                            callback("登录账号所属公众号与本APP不符！");
                            return;
                        }
                        break;

                }
            }else{
                console.log('session_change======res.productKey=',res.data.productKey);
                dispatch({type:types.CHANGE, session:null});
                callback("获取不到登录账号所属公众号信息");
                return;
            }



            let product = await api.post('/config/getProductByT',{ticket:ticket});
            console.log('session_change======product',product);
            let user = {}
            if (product.cd == 0) {
                let p = product.data;
                user = {ticket, ...res.data,group:group,product:{...p}};
                AsyncSession.saveTokenAndUser(ticket, user)
                .then((data)=>{
                    dispatch({type:types.CHANGE, session:{...data, reload}});
                });
            }else{
                user = {ticket, ...res.data,group:group};
                AsyncSession.saveTokenAndUser(ticket, user)
                .then((data)=>{
                    dispatch({type:types.CHANGE, session:{...data, reload}});
                });
            }
        }
    };
}

function session_data_change(session){
    return async (dispatch) =>{

        AsyncSession.saveTokenAndUser(session.stoken, session.user)
        .then((data)=>{
            dispatch({type:types.CHANGE, session:{...data, reload: session.reload}});
        });
    };
}

function clear_session(){
    return async (dispatch) =>{
        await AsyncSession.clearTokenAndUser();
        dispatch({type:types.CHANGE, session:null});
    }
}

module.exports = {session_change, clear_session, session_data_change};
